package com.ebt.m.proposal_v2.helper;

import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.ebt.m.customer.constant.Dictionary;
import com.ebt.m.customer.view.NumberPickerDialog;
import com.ebt.m.proposal_v2.adapter.ProductInProposalAdapter;
import com.ebt.m.proposal_v2.adapter.ProductInProposalAdapter2;
import com.ebt.m.proposal_v2.bean.ArgProductOption;
import com.ebt.m.proposal_v2.widget.dialog.EBTBottomAdapterDialog;
import com.ebt.m.proposal_v2.widget.dialog.EBTBottomListDialog;
import com.ebt.m.proposal_v2.widget.dialog.EBTBottomWebDialog;
import com.ebt.m.proposal_v2.widget.dialog.EBTConfirmDialogWithTitle;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.proposal_v2.widget.dialog.EBTTipDialog;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 封装一系列的Dialog,用于选择年龄、生日等
 *
 * @author archer.qi
 *         Created on 2017/5/9.
 */
public class PickerHelper {
    /* 职业类别*/
    private static final int TAG_CAREER_CATEGORY = 0;
    /* 教育程度*/
    private static final int TAG_EDUCATION_GRADING = 1;
    /* 年龄*/
    private static final int TAG_AGE = 2;
    /* 等级*/
    private static final int TAG_LEVEL = 3;

    public static final int MAX_AGE = 120;
    private static String[] displayAges;

    /**
     * 选择年龄
     *
     * @param context
     * @param defaultValue
     * @param listener
     */
    public static NumberPickerDialog chooseAge(Context context, int defaultValue, NumberPickerDialog.SelectedListener listener) {
        final String title = "年龄";
        if (displayAges == null) {
            displayAges = new String[MAX_AGE + 1];
            for (int i = 0; i < MAX_AGE + 1; i++) {
                displayAges[i] = String.valueOf(i);
            }
        }
        NumberPickerDialog mNumberPickerDialog = new NumberPickerDialog(context, title, displayAges, defaultValue, TAG_AGE);
        mNumberPickerDialog.setSelectedListener(listener);
        mNumberPickerDialog.show();
        return mNumberPickerDialog;
    }

    /**
     * 选择年龄（新版）
     *
     * @param manager
     * @param defaultValue
     * @param listener
     * @return
     */
    public static EBTBottomListDialog pickAge(FragmentManager manager, boolean insured, int defaultValue, EBTBottomListDialog.OnSingleCheckListener listener, ArrayList<Integer> limitedDataIndex) {
        EBTBottomListDialog mAgeDialog = EBTBottomListDialog.newInstance(insured ? Dictionary.INSURED_AGE_LIST : Dictionary.APPLICANT_AGE_LIST, limitedDataIndex);
        mAgeDialog.setTitle("选择年龄");
        mAgeDialog.setDefaultCheckValue(defaultValue + "岁");
        mAgeDialog.addOnSingleCheckListener(listener);
        mAgeDialog.show(manager, "AGE");
        return mAgeDialog;
    }

    /**
     * 选择生日
     *
     * @param context
     * @param listener
     */
    public static DatePickerDialog chooseBirthday(Context context, DatePickerDialog.OnDateSetListener listener) {
        Calendar c = Calendar.getInstance();
        DatePickerDialog mDatePickerDialog = new DatePickerDialog(context, listener,
                c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
        mDatePickerDialog.show();
        return mDatePickerDialog;
    }

    /**
     * 选择生日
     *
     * @param context
     * @param listener
     */
    public static DatePickerDialog chooseBirthday(Context context, int defaultYear, int defaultMonth, int defaultDay, DatePickerDialog.OnDateSetListener listener) {
        DatePickerDialog mDatePickerDialog = new DatePickerDialog(context, listener,
                defaultYear, defaultMonth, defaultDay);
        mDatePickerDialog.show();
        return mDatePickerDialog;
    }

    //选择职业类别
    public static EBTBottomListDialog pickCareer(FragmentManager manager, String defaultValue, EBTBottomListDialog.OnSingleCheckListener listener, ArrayList<Integer> limitedDataIndex) {
        EBTBottomListDialog mCareerDialog = EBTBottomListDialog.newInstance(Dictionary.CAREER_LEVEL_LIST, limitedDataIndex);
        mCareerDialog.setTitle("选择职业类型");
        mCareerDialog.setDefaultCheckValue(defaultValue);
        mCareerDialog.addOnSingleCheckListener(listener);
        mCareerDialog.show(manager, "CAREER");
        return mCareerDialog;
    }

    //选择被保人与投保人的关系
    public static EBTBottomListDialog pickRelation(FragmentManager manager, String defaultValue, EBTBottomListDialog.OnSingleCheckListener listener, ArrayList<Integer> limitedIndex) {
        EBTBottomListDialog mCareerDialog = EBTBottomListDialog.newInstance(Dictionary.RELATION_LIST, limitedIndex);
        mCareerDialog.setTitle("投被保人关系");
        mCareerDialog.setDefaultCheckValue(defaultValue);
        mCareerDialog.addOnSingleCheckListener(listener);
        mCareerDialog.show(manager, "RELATION");
        return mCareerDialog;
    }

    /**
     * 确认窗口
     *
     * @param confirmMsg
     * @param onDeleteListener
     * @return
     */
    public static EBTConfirmDialogWithTitle confirm(String confirmMsg, String positiveText, View.OnClickListener onDeleteListener) {
        EBTConfirmDialogWithTitle dialog = new EBTConfirmDialogWithTitle();
        dialog.setMessage(confirmMsg);
        if (onDeleteListener != null) {
            dialog.setPositiveButton(positiveText, onDeleteListener);
        }
        return dialog;
    }

    /**
     * 提示窗口
     *
     * @param confirmMsg
     * @return
     */
    public static EBTTipDialog tip(String confirmMsg) {
        return new EBTTipDialog().setMessage(confirmMsg);
    }

    /**
     * 开始加载
     *
     * @param context
     * @param msg
     * @return
     */
    public static EBTProgressDialog showProgress(Context context, String msg) {
        EBTProgressDialog dialog = new EBTProgressDialog(context);
        dialog.setMessage(msg);
        dialog.show();
        return dialog;
    }

    /**
     * 计划书主附险编辑计算器
     *
     * @param manager
     * @param option  参数
     * @return
     */
    public static EBTBottomWebDialog showProposalCalculator(FragmentManager manager, ArgProductOption option) {
        EBTBottomWebDialog dialog = EBTBottomWebDialog.getInstance(option);
        dialog.show(manager, "proposal-main-or-attach-calculator");
        return dialog;
    }

    /**
     * 计划书制作产品资料窗口
     *
     * @param manager
     * @param adapter
     * @return
     */
    public static EBTBottomAdapterDialog showProductsInProposalDialog(FragmentManager manager, ProductInProposalAdapter adapter) {
        if (adapter == null) {
            throw new IllegalArgumentException("adapter is null, it's need not to open a dialog with no-data");
        }
        EBTBottomAdapterDialog<ProductInProposalAdapter> dialog = new EBTBottomAdapterDialog<>();
        dialog.setAdapter(adapter);
        dialog.setTitle("产品列表");
        dialog.show(manager, "products-dialog-in-proposal-making");
        return dialog;
    }

    /**
     * 计划书制作产品资料窗口
     *
     * @param manager
     * @param adapter
     * @return
     */
    public static EBTBottomAdapterDialog showProductsInProposalDialog2(FragmentManager manager, ProductInProposalAdapter2 adapter) {
        if (adapter == null) {
            throw new IllegalArgumentException("adapter is null");
        }
        EBTBottomAdapterDialog<ProductInProposalAdapter2> dialog = new EBTBottomAdapterDialog<>();
        dialog.setAdapter(adapter);
        dialog.setTitle("产品列表");
        dialog.show(manager, "products-dialog-in-proposal-making");
        return dialog;
    }


}
